//
//  XDScheduleAddViewController.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-6-21.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "XDScheduleAddViewController.h"
#import "EpointDatePickerAlertView.h"
#import "DateUtil.h"
#import "ScheduleInfo.h"
#import "AddScheduleTask.h"
#import "EditScheduleTask.h"

@interface XDScheduleAddViewController ()
{
    int datepickflag;
    NSMutableArray *arrZyx;
    NSString *editFlag;
    NSString *addScheduleTaskGuid,*editScheduleTaskGuid;
    ScheduleInfo *m;
}
@end

@implementation XDScheduleAddViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    arrZyx = [[NSMutableArray alloc]initWithObjects:@"无",@"低",@"高", nil];
    UIButton* rightButton= [[UIButton alloc] initWithFrame:CGRectMake(0,0,40,20)];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"img_top_save"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(saveSchedule) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = leftBarButtonItem;
    [leftBarButtonItem release];
    [rightButton release];
    [self.sv setContentSize:CGSizeMake(320, 620)];
    
    editFlag = [[intentParams objectForKey:@"editFlag"]retain];
    if ([editFlag isEqualToString:@"0"]) {
        self.title = @"日程添加";
        
        
        
        NSDate *ndate = [NSDate date];
        
        NSString *now = [DateUtil convertDate2FormatStr:ndate format:@"mm"];
        int nowMin = now.intValue;
        
        if (nowMin>=0&&nowMin<=30)
        {
            NSString *kssjq = [DateUtil convertDate2FormatStr:ndate format:@"yyyy-MM-dd HH"];
            NSString *kssj = [NSString stringWithFormat:@"%@:30",kssjq];
            NSLog(@"kssj:%@",kssj);
            
            NSDate *kssjdate = [DateUtil convertStr2Date:kssj format:@"yyyy-MM-dd HH:mm"];
            NSDate *jssjdate= [ndate initWithTimeInterval:30*60 sinceDate:kssjdate];
            NSString *jssj = [DateUtil convertDate2FormatStr:jssjdate format:@"yyyy-MM-dd HH:mm"];
            NSLog(@"jssj:%@",jssj);
            
            [self.btFromDate setTitle:kssj forState:UIControlStateNormal];
            [self.btToDate setTitle:jssj forState:UIControlStateNormal];
        }
        else
        {
            NSDate *d= [ndate initWithTimeInterval:1*60*60 sinceDate:ndate];
            NSString *kssjq = [DateUtil convertDate2FormatStr:d format:@"yyyy-MM-dd HH"];
            NSString *kssj = [NSString stringWithFormat:@"%@:00",kssjq];
            NSLog(@"kssj:%@",kssj);
            NSDate *kssjdate = [DateUtil convertStr2Date:kssj format:@"yyyy-MM-dd HH:mm"];
            
            NSDate *jssjdate= [ndate initWithTimeInterval:30*60 sinceDate:kssjdate];
            NSString *jssj = [DateUtil convertDate2FormatStr:jssjdate format:@"yyyy-MM-dd HH:mm"];
            NSLog(@"jssj:%@",jssj);
            [self.btFromDate setTitle:kssj forState:UIControlStateNormal];
            [self.btToDate setTitle:jssj forState:UIControlStateNormal];
        }
        
        
        
        
    }
    else if([editFlag isEqualToString:@"1"])
    {
        self.title = @"日程修改";
        m = [intentParams objectForKey:@"model"];
        self.btSubject.text = m.Subject;
        self.btAddress.text = m.Location;
        [self.btFromDate setTitle:m.FromDateTime forState:UIControlStateNormal];
        [self.btToDate setTitle:m.ToDateTime forState:UIControlStateNormal];
        NSString *importanceStr = @"无";
        if ([m.Importance isEqualToString:@"0"]) {
            importanceStr = @"低";
        }
        else if([m.Importance isEqualToString:@"2"])
        {
            importanceStr = @"高";
        }
        [self.btZYX setTitle:importanceStr forState:UIControlStateNormal];
        [self.tvContent setText:m.ActivityDescription];
    }
}

-(void)saveSchedule
{
    NSDate *kssjdate = [DateUtil convertStr2Date:self.btFromDate.titleLabel.text format:@"yyyy-MM-dd HH:mm"];
    NSDate *jssjdate = [DateUtil convertStr2Date:self.btToDate.titleLabel.text format:@"yyyy-MM-dd HH:mm"];
    
    if ([[kssjdate earlierDate:jssjdate]isEqualToDate:jssjdate]) {
        [UITool toast:@"开始时间需要在结束时间之前!"];
        return;
    }
    
    NSString *subject = self.btSubject.text;
    NSString *address = self.btAddress.text;
    NSString *fromtime = self.btFromDate.titleLabel.text;
    NSString *totime = self.btToDate.titleLabel.text;
    NSString *importance = @"1";
    NSString *content = self.tvContent.text;
    NSLog(@"%@",subject);
    NSLog(@"%@",address);
    NSLog(@"%@",fromtime);
    NSLog(@"%@",totime);
    NSLog(@"%@",importance);
    NSLog(@"%@",content);
    if ([self.btZYX.titleLabel.text isEqualToString:@"低"]) {
        importance = @"0";
    }
    else if([self.btZYX.titleLabel.text isEqualToString:@"高"])
    {
        importance = @"2";
    }
    
    NSString *IFNotice = @"0";
    if (cbMsg.isOn) {
        IFNotice = @"1";
    }
    
    
    NSMutableDictionary *params = self.getPassDictionary;
    [params setObject:IFNotice forKey:@"IFNotice"];
    [params setObject:self.btSubject.text forKey:@"Subject"];
    [params setObject:self.btAddress.text forKey:@"Location"];
    [params setObject:self.tvContent.text forKey:@"ActivityDescription"];
    [params setObject:importance forKey:@"Importance"];
    [params setObject:self.btFromDate.titleLabel.text forKey:@"FromDateTime"];
    [params setObject:self.btToDate.titleLabel.text forKey:@"ToDateTime"];
    [self showMBPHUDWithMessage:@"正在保存..."];
    if ([editFlag isEqualToString:@"0"]) {

        addScheduleTaskGuid = [[[AddScheduleTask alloc]initWithContext:self andParams:params]startTask];
    }
    else if([editFlag isEqualToString:@"1"])
    {
        [params setObject:m.ActivityID forKey:@"ActivityID"];
        editScheduleTaskGuid = [[[EditScheduleTask alloc]initWithContext:self andParams:params]startTask];
    }
}

-(void)refreshUIWithThreadGuid:(NSString *)threadGuid andData:(NSObject *)obj
{
    [self hideMBPHUD];
    
//    if ([threadGuid isEqualToString:addScheduleTaskGuid]) {
        NSString *bs = (NSString *)obj;
        if([self validateXML:bs])
        {
            [UITool toast:@"保存成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
//    }
}

- (IBAction)chooseFromDate:(id)sender {
    EpointDatePickerAlertView *p = [[EpointDatePickerAlertView alloc]init];
    [self.view addSubview:p];
    p.delegate = self;
    datepickflag = 1;
}
- (IBAction)chooseToDate:(id)sender {
    EpointDatePickerAlertView *p = [[EpointDatePickerAlertView alloc]init];
    p.delegate = self;
    [self.view addSubview:p];
    datepickflag = 2;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_sv release];
    [_btFromDate release];
    [_btToDate release];
    [_btZYX release];
    [_btSubject release];
    [_btAddress release];
    [_tvContent release];
    [cbMsg release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setSv:nil];
    [self setBtFromDate:nil];
    [self setBtToDate:nil];
    [self setBtZYX:nil];
    [self setBtSubject:nil];
    [self setBtAddress:nil];
    [self setTvContent:nil];
    [cbMsg release];
    cbMsg = nil;
    [super viewDidUnload];
}

#pragma textview delegate

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        [self.sv setContentOffset:CGPointMake(0, 100) animated:YES];
        return NO;
    }
    return YES;
}
- (IBAction)textFieldDidEndExit:(id)sender {
    [sender resignFirstResponder];
}
- (IBAction)zyxAction:(id)sender {
    EpointSingleChooseAlertView *alert = [[EpointSingleChooseAlertView alloc]initWithArray:arrZyx title:@"请选择日程重要性"];
    [self.view addSubview:alert];
    alert.delegate = self;
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    [self.sv setContentOffset:CGPointMake(0, 260) animated:YES];
    return YES;
}

-(void)epointDatePickerSelected:(EpointDatePickerAlertView *)epointDatePickerAlertView
{
    if (datepickflag==1) {
        [self.btFromDate setTitle:[DateUtil getTimeStr:epointDatePickerAlertView.p.date] forState:UIControlStateNormal];
    }
    else if(datepickflag==2)
    {
        [self.btToDate setTitle:[DateUtil getTimeStr:epointDatePickerAlertView.p.date] forState:UIControlStateNormal];
    }
}

-(void)epointSingleChooseViewEndSelected:(EpointSingleChooseAlertView *)epointSingleChooseAlertView
{
    [self.btZYX setTitle:[arrZyx objectAtIndex:epointSingleChooseAlertView.selectedIndex] forState:UIControlStateNormal];
}

@end

